package com.hgb.mall.admin.services.impl;

import com.github.pagehelper.PageHelper;
import com.hgb.mall.admin.services.OmsOrderReturnReasonService;
import com.hgb.mall.mbg.entity.OmsOrderReturnReason;
import com.hgb.mall.mbg.mapper.OmsOrderReturnReasonDynamicSqlSupport;
import com.hgb.mall.mbg.mapper.OmsOrderReturnReasonMapper;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static org.mybatis.dynamic.sql.SqlBuilder.isIn;

/**
 * 订单原因管理Service实现类
 * Created by hgb on 2020/10/17.
 */
@Service
public class OmsOrderReturnReasonServiceImpl implements OmsOrderReturnReasonService {
    @Autowired
    private OmsOrderReturnReasonMapper returnReasonMapper;

    @Override
    public int create(OmsOrderReturnReason returnReason) {
        returnReason.setCreateTime(new Date());
        return returnReasonMapper.insert(returnReason);
    }

    @Override
    public int update(Long id, OmsOrderReturnReason returnReason) {
        returnReason.setId(id);
        return returnReasonMapper.updateByPrimaryKey(returnReason);
    }

    @Override
    public int delete(List<Long> ids) {

        return returnReasonMapper.delete(c -> c.where(OmsOrderReturnReasonDynamicSqlSupport.id, isIn(ids)));
    }

    @Override
    public List<OmsOrderReturnReason> list(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);

        return returnReasonMapper.select(c -> c.orderBy(OmsOrderReturnReasonDynamicSqlSupport.sort.descending()));
    }

    @Override
    public int updateStatus(List<Long> ids, Integer status) {
        if (!status.equals(0) && !status.equals(1)) {
            return 0;
        }
        UpdateStatementProvider updateStatement = SqlBuilder
                .update(OmsOrderReturnReasonDynamicSqlSupport.omsOrderReturnReason)
                .set(OmsOrderReturnReasonDynamicSqlSupport.status).equalToWhenPresent(status)
                .where(OmsOrderReturnReasonDynamicSqlSupport.id, isIn(ids))
                .build()
                .render(RenderingStrategies.MYBATIS3);
        return returnReasonMapper.update(updateStatement);
    }

    @Override
    public OmsOrderReturnReason getItem(Long id) {
        return returnReasonMapper.selectByPrimaryKey(id).orElse(null);
    }
}
